﻿using System.Data;
using System.Collections.Generic;
using System.Text;
using KPIS.GERP.GAMS.MODEL;
using KPIS.GERP.GAMS.DAL;
using KPIS.DBM;
using System.Configuration;
using System;

namespace KPIS.GERP.GAMS.DAL
{
    public class D_ACM_Finance_Receive
    {
        DBManager dbManager = new DBManager(SystemInfo._DataProvider, ConfigurationSettings.AppSettings["ConnectionString"].ToString());
        ConvertNullable cn = new ConvertNullable();

        public IList<M_ACM_FIN_MAS_RECEIVE_Type> GetList() //IList<M_ACM_FIN_MAS_RECEIVE_Type>  GetList(Nullable<int> version_seq)
        {
            DataRowCollection drc = null;
            IList<M_ACM_FIN_MAS_RECEIVE_Type> infoList = new List<M_ACM_FIN_MAS_RECEIVE_Type>();
            try
            {
                dbManager.Open();
                
                string strSQL = " SELECT receive_type_seq,receive_type_name,fin.record_status,fin.bgm_source_seq,bgm_source_code,bgm_source_name,fin.updated_by,fin.updated_when,acc.account_name "
                               + " ,concat(coalesce(acc.account_code, ''), ' : ', coalesce(acc.account_name, '')) as acc_name,fin.account_seq"
                               + " FROM Fim_mas_receive_types fin "
                               + " left join  bgm_mas_source bgm on fin.bgm_source_seq = bgm.bgm_source_seq"
                               + " left join  acm_mas_account acc on fin.account_seq = acc.account_seq"
                               + " and acc.record_status = 'U' "
                               + " left join acm_mas_account_version acc_ver on acc.version_seq = acc_ver.version_seq"
                               + " and acc_ver.record_status = 'U' "
                               + " and acc_ver.use_flag = 1"
                               + " WHERE  fin.record_status not like 'D'"
                               + " ORDER BY receive_type_name" ;
                    

                drc = dbManager.ExecuteDataSet(CommandType.Text, strSQL).Tables[0].Rows;
                if (drc.Count > 0)
                {
                    for (int i = 0; i < drc.Count; i++)
                    {
                        M_ACM_FIN_MAS_RECEIVE_Type info = new M_ACM_FIN_MAS_RECEIVE_Type();
                        info.receive_type_seq = Convert.ToInt32(drc[i]["receive_type_seq"]);
                        info.receive_type_name = drc[i]["receive_type_name"].ToString();
                        info.bgm_source_seq = Convert.ToInt32(drc[i]["bgm_source_seq"]);
                        info.bgm_source_code = drc[i]["bgm_source_code"].ToString();
                        info.bgm_source_name = drc[i]["bgm_source_name"].ToString();
                        info.account_seq = cn.ConvertNullInt(drc[i]["account_seq"]);
                        info.account_name = drc[i]["acc_name"].ToString();
                        infoList.Add(info);
                    }
                }
            }
            catch (Exception ex)
            {
                throw (ex);
            }
            finally
            {
                dbManager.Dispose();
            }

            return infoList;

        }

        public M_ACM_FIN_MAS_RECEIVE_Type GetListBySeq(Nullable<int> receive_type_seq)
        {
            DataRowCollection drc = null;
            M_ACM_FIN_MAS_RECEIVE_Type info = new M_ACM_FIN_MAS_RECEIVE_Type();
            string strSQL =  "";
            try
            {
                
                dbManager.Open();
                dbManager.CreateParameters(1);
                dbManager.AddParameters(0, "@receive_type_seq", receive_type_seq);

                strSQL =   " SELECT receive_type_seq,receive_type_name,fin.record_status,fin.bgm_source_seq,bgm_source_code,bgm_source_name,fin.updated_by,fin.updated_when "
                         + " ,concat(coalesce(acc.account_code, ''), ' : ', coalesce(acc.account_name, '')) as acc_name ,fin.account_seq"
                         + " FROM Fim_mas_receive_types fin "
                         + " left join  bgm_mas_source bgm on fin.bgm_source_seq = bgm.bgm_source_seq"
                         + " left join  acm_mas_account acc on fin.account_seq = acc.account_seq"
                         + " and acc.record_status = 'U' "
                         + " left join acm_mas_account_version acc_ver on acc.version_seq = acc_ver.version_seq"
                         + " and acc_ver.record_status = 'U' "
                         + " and acc_ver.use_flag = 1"
                         + " WHERE  fin.record_status not like 'D' and"
                         + " receive_type_seq = @receive_type_seq"
                         + " ORDER BY receive_type_name " ;

                drc = dbManager.ExecuteDataSet(CommandType.Text, strSQL).Tables[0].Rows;
                if (drc.Count > 0)
                {
                    info.receive_type_seq = Convert.ToInt32(drc[0]["receive_type_seq"]);
                    info.receive_type_name = drc[0]["receive_type_name"].ToString();
                    info.bgm_source_seq = cn.ConvertNullInt(drc[0]["bgm_source_seq"]);
                    info.bgm_source_code = drc[0]["bgm_source_code"].ToString();
                    info.bgm_source_name = drc[0]["bgm_source_name"].ToString();
                    info.account_seq = cn.ConvertNullInt(drc[0]["account_seq"]);
                    info.account_name = drc[0]["acc_name"].ToString();   
                }
            }

            catch (Exception ex)
            {
                throw (ex);
            }
            finally
            {
                dbManager.Dispose();
            }
            return info;
        }


        public void Update(M_ACM_FIN_MAS_RECEIVE_Type info)
        {
            string strSQL;
            try
            {
                dbManager.Open();
                dbManager.CreateParameters(4);

                dbManager.AddParameters(0, "@receive_type_seq", info.receive_type_seq);
                dbManager.AddParameters(1, "@account_seq", info.account_seq);
                dbManager.AddParameters(2, "@updated_by", info.updated_by);
                dbManager.AddParameters(3, "@updated_when", info.updated_when);

                strSQL = "UPDATE fim_mas_receive_types"
                    + "  SET account_seq = @account_seq"
                    + ", updated_by = @updated_by"
                    + ", updated_when = @updated_when"
                    + " WHERE receive_type_seq = @receive_type_seq";
                dbManager.ExecuteNonQuery(CommandType.Text, strSQL);
            }
            catch (Exception ex)
            {
                throw (ex);
            }
            finally
            {
                dbManager.Dispose();
            }
        }

        public DataTable Report_FINANCE_Receive()
        {
            string strSQL;
            DataTable dtb = null;
            try
            {
                dbManager.Open();

                strSQL =    " SELECT receive_type_name,fin.bgm_source_seq,bgm_source_code,bgm_source_name,acc.account_name ,acc.account_code "
                          + " FROM Fim_mas_receive_types fin "
                          + " left join  bgm_mas_source bgm on fin.bgm_source_seq = bgm.bgm_source_seq "
                          + " left join  acm_mas_account acc on fin.account_seq = acc.account_seq "
                          + " and acc.record_status = 'U' "
                          + " left join acm_mas_account_version acc_ver on acc.version_seq = acc_ver.version_seq "
                          + " and acc_ver.record_status = 'U' "
                          + " and acc_ver.use_flag = 1 "
                          + " WHERE  fin.record_status not like 'D' "
                          + " ORDER BY receive_type_name ";

                dtb = dbManager.ExecuteDataSet(CommandType.Text, strSQL).Tables[0];
            }
            catch (Exception ex)
            {
                throw (ex);
            }
            finally
            {
                dbManager.Dispose();
            }

            return dtb;
        }


    }
}
